package com.fantasy.string;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

public class StringUtils {

	public static boolean contrast(String str , String str2 , boolean sensitive){
		if(str == null){
			str = "";
		}
		if(str2 == null){
			str2 = "";
		}
		str = str.trim();
		str2 = str2.trim();
		if(!sensitive){
			str = str.toUpperCase();
			str2 = str2.toUpperCase();
		}
		if(str.equals(str2)){
			return true;
		}else{
			return false;
		}
	}
	
	public static boolean contrastNoSensitive(String str , String str2){
		return contrast(str, str2, false);
	}
	
	public static String[] addStringArray(String[] a, String b) {
		List<String> list = new ArrayList<String>(Arrays.asList(a));
		list.addAll(Arrays.asList(b));
		return (String[]) list.toArray();
	}
	
	public static String reMoveSpecialChar(String str) {
		String regEx = "[-_`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		return m.replaceAll("").trim();
	}

	public static ArrayList<String> getStrings(String str, String regex) {
		//System.out.println("查找字符串：" + str + " 正则：" + regex );
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(str);
		ArrayList<String> strs = new ArrayList<String>();
		while (m.find()) {
			strs.add(m.group(0));
		}
		return strs;
	}
	
	@Test
	public void testGetString(){
		for(String str : getStrings("aa{aa=aaa{aaaaaa=bbbb}4444}", "\\{[^}]*\\}")){
			System.out.println(str);
		}
	}
}
